/**
 * @package     org.routenova.csv2db
 * @version     $Id: CSV2db.java 2 2007-01-16 19:13:36Z mikhail.branicki $
 * @copyright   (c) 2007 RouteNova.org 
 * @license     http://www.routenova.org/License - new BSD License 
 */
package org.routenova.csv2db;

import java.io.FileInputStream;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Properties;

import au.com.bytecode.opencsv.*;

/**
 * Application read CSV file, parse data and insert to database.
 * @author      Mikhail Branicki <mikhail.branicki@gmail.com> 
 * @param args  CSV file name.
 * TODO: Put error processing functions.
 * TODO: Put execution process output functions.
 */
public class CSV2db {
    private static Connection DB;
    private static String sheme = "";
    private static String table = "";
    /**
     * @param args  CSV file name.
     */
    public static void main(String[] args) {
        
        String hostname = "";
        String username = "";
        String password = "";
        String db_driver = "";
        String dbname = "";
        
        Properties config_data = new Properties();
        //Read configuration data
        try {
            
            FileInputStream conf_file = new FileInputStream("config.cfg");
            config_data.load(conf_file);
            
            hostname = config_data.getProperty("hostName");
            username = config_data.getProperty("userName");
            password = config_data.getProperty("password");
            sheme = config_data.getProperty("sheme");
            db_driver = config_data.getProperty("db_driver");
            table = config_data.getProperty("table");
            dbname = config_data.getProperty("db");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        
        String conn = "jdbc:postgresql://" + hostname + "/" +dbname;
        //Connect to database
        
        try {
            Class.forName(db_driver);
            DB = DriverManager.getConnection(conn, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
        CSVReader reader = new CSVReader(new FileReader(args[0]));

        String [] nextLine;
        while ((nextLine = reader.readNext()) != null) {

            add(nextLine);
        }
        } catch (Exception e) {
            e.printStackTrace();
        }
        

    }
    /**
     * Insert data to database
     * @param   data    Data fields
     * @return
     */
    public static boolean add(String[] data) {
        PreparedStatement stmt = null;
        boolean isDone = false;
        boolean first = true;
        Integer size,index;
        String divider = ",";
        String singleparam = "";
        size = data.length;
        index = 0;
        String query = "";
        try {
        while (size > index) {
            if(first) {
                divider = "";
            } else {
                divider = ",";
            }
            first = false;
            singleparam = "'" + data[index] + "'";
            query = query + divider + singleparam;
            index++;
        }
        } catch (Exception e) {
            e.printStackTrace();
        }

        /* Insert new contact*/
        try {
            stmt = DB.prepareStatement("INSERT INTO "
                            + sheme + "."
                            + table
                            + " VALUES (" + query + ")");
            stmt.executeUpdate();

            DB.commit();
            
        } catch (Exception e) {
            e.printStackTrace();
        }

        query = "";
        return true;
       
    }

}
